package com.wct.springSecurity.util;

import java.util.Date;
import java.util.Random;

/**
 * @author WEI (Email:changtan20000@163.com)
 * @description 密码工具类
 * @date 2020/1/17 15:16
 */
public class PasswordUtil {

    // 能使用的字符集
    public final static String[] word = {
            "a", "b", "c", "d", "e", "f", "g",
            "h", "j", "k", "m", "n",
            "p", "q", "r", "s", "t",
            "u", "v", "w", "x", "y","z",
            "A", "B", "C", "D", "E", "F", "G",
            "H", "J", "K", "M", "N",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "Z", "Y", "Z"
    };
    // 数字
    public final static String[] num = {
      "2", "3", "4", "5", "6", "7", "8", "9"
    };

    private static String randomPassword() {
        StringBuilder stringBuffer = new StringBuilder();

        Random random = new Random(new Date().getTime());

        boolean flag = false;
        int length = random.nextInt(3) + 8;

        for (int i = 0; i < length; i++) {
            if (flag) {
                stringBuffer.append(num[random.nextInt(num.length)]);
            }else {
                stringBuffer.append(word[random.nextInt(word.length)]);
            }

            // 字母和数字交替出现
//            flag = !flag;
            //字母和数字随机出现
            flag = random.nextBoolean();
        }

        return stringBuffer.toString();
    }

    public static void main(String[] args) throws Exception{
        System.out.println(randomPassword());
        Thread.sleep(100);
        System.out.println(randomPassword());
        Thread.sleep(100);
        System.out.println(randomPassword());
        Thread.sleep(100);
        System.out.println(randomPassword());
    }
}
